import re

src_list = []


class Index:
    idx_apiOperation = 0
    idx_mapping = 1
    idx_method = 2
    idx_roles = 4
    idx_result = idx_roles

    @property
    def idx_result(self):
        return self.idx_roles

    def __init__(self):
        self.idx_apiOperation = 0
        self.idx_mapping = 1
        self.idx_method = 2
        self.idx_roles = 4


def printer(src_list: list):
    for line in src_list:
        print(line)


def capitalize(word: str):
    if len(word) < 1: return word
    return word[0].capitalize() + word[1:]


def main():
    index = Index()
    src_list = [
        '@ApiOperation("0")',
        '@0Mapping("1")',
        'public Result 0(1) {',
        '\ttry {',
        '\t\treturn new Result(ResultCode.SUCCESS, 0.1(2));',
        '\t} catch (Exception e) {',
        '\t\te.printStackTrace();',
        '\t\treturn new Result(ResultCode.FAILED, e.getMessage());',
        '\t}',
        '}'
    ]

    # # test
    # introduction = "修改条例, 使用条例 id"
    # method = "post"
    # url = "/regulationDetail/modify/{pathVariable}"
    # args_list = "'@a(条例详情 设置条例 id) @rb RegulationDetail regulation, @a(测试信息) @rp String testMessage, @a(同上) @pv String pathVariable'".strip('\'')
    # service_name = "webScoreService"
    # funcs = 'all("admin")'.strip('\'')
    introduction = input("请输入方法介绍：")
    method = input("请输入请求方式：")
    url = input("请输入 url 路径：")
    args_list = input("请输入参数列表(String arg1, int arg2)：").strip('\'')
    service_name = input("请输入 service 名称：")
    funcs = input("请输入自定义函数：").strip('\'')
    new_list = src_list.copy()
    new_list[index.idx_apiOperation] = new_list[index.idx_apiOperation].replace("0", introduction)
    new_list[index.idx_mapping] = new_list[index.idx_mapping].replace("0", capitalize(method)).replace("1", url)
    args_list = args_list \
        .replace('@a(', '@ApiParam("') \
        .replace(')', '")') \
        .replace('@rp', '@RequestParam') \
        .replace('@rb', '@RequestBody') \
        .replace('@pv', '@PathVariable')
    method_name = ''
    url_split = url.split('/')
    for i in range(len(url_split)):
        if url_split[i].startswith('{'):
            continue
        method_name = url_split[i] + capitalize(method_name)
    new_list[index.idx_method] = new_list[index.idx_method].replace("0", method_name).replace("1", args_list)
    for f in funcs.split(';'):
        if f.startswith('all'):
            f = f.replace('all(', '')
            f = f.replace(')', '')
            new_list.insert(index.idx_roles, '\t\tRoleAssert.hasAllRole({0});'.format(f))
            index.idx_roles += 1
        elif f.startswith('one'):
            f = f.replace('one(', '')
            f = f.replace(')', '')
            new_list.insert(index.idx_roles, '\t\tRoleAssert.hasOneRole({0});'.format(f))
            index.idx_roles += 1
    args = args_list.split(',')
    args_str = ''
    if args_list != '':
        for i in range(len(args)):
            if i == 0:
                args_str = args[i].split()[-1]
            else:
                args_str += ', ' + args[i].split()[-1]
    new_list[index.idx_result] = new_list[index.idx_result].replace("0", service_name) \
        .replace("1", method_name) \
        .replace("2", args_str)
    printer(new_list)


if __name__ == '__main__':
    while True:
        main()
        input()
